package com.ccll.domain.common;

import com.ccll.constants.GlobalConstant;
import com.ccll.domain.dto.LoginUser;
import jdk.nashorn.internal.objects.Global;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @author Seon
 * @date 2022/10/19 23:28
 */
@Slf4j
public class BaseContextHolder {
    /**
     * 把SpringSecurity的代码封装了一下。
     *
     * @return 当亲用户id
     */
    public static Long getCurrentId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object principal = authentication.getPrincipal();
        if (principal.equals("anonymousUser")) {
            return 0L;
        }
        LoginUser loginUser = (LoginUser) principal;
        return loginUser.getUser().getId();
    }
}
